package cn.wgx.module.ent.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.base.IBaseNoneController;
import cn.wgx.common.entity.ent.EntUser;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.ent.service.EntUserService;
import cn.wgx.module.ent.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "5.2 企业用户管理")
@RestController
@RequestMapping("/api/entUser")
public class EntUserCtrl extends IBaseController<EntUserService, EntUser> {

    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation(value = "查询企业用户,分页查询(page第几页, limit每页数量,默认20). ")
    @GetMapping("list")
    public Map list(@ModelAttribute EntUser entUser,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getEntUsers(page1,
                new QueryWrapper<>(entUser), entUser.getNickname()));
    }

    @ApiOperation(value = "查询企业不包含已加入的人,根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20). ")
    @GetMapping("NotInEntList")
    public Map listNotIn(@RequestParam Integer entId,
                         @RequestParam(required = false) String nickName,
                        @RequestParam(required = false, defaultValue = "1") Integer page,
                        @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getNotInEntUsers(page1, entId, nickName));
    }

    @ApiOperation(value = "新增实体数据-批量")
    @PostMapping("addBatch")
    public Map addBatch(@Validated @RequestBody List<EntUser> t) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        for(EntUser entUser: t){
            Integer createUserIdById = enterpriseService.getCreateUserIdById(entUser.getEnt_id());
            if(!loginAppUser.getUser_id().equals(createUserIdById)){
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
        }
        service.saveBatch(t);
        return ResponseDataUtil.returnData(t);
    }

    @ApiOperation(value = "根据主键ID删除数据-批量", notes = "必填：eu_id")
    @DeleteMapping("delBatch")
    public Map delBatch(@RequestParam List<Integer> ids) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        List<EntUser> entUsers = service.listByIds(ids);
        for (EntUser entUser : entUsers) {
            if(entUser.getEnt_id().equals(1)){
                return ResponseDataUtil.returnErrorMap("个人空间不可以操作");
            }
            if (!loginAppUser.getUser_id().equals(1)
                    && (!loginAppUser.getUser_id().equals(enterpriseService.getCreateUserIdById(entUser.getEnt_id()))) ) {
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
        }
        if(entUsers.size() > 0){
            return ResponseDataUtil.returnData(service.removeByIds(ids));
        }
        return ResponseDataUtil.returnErrorMap("id错误");
    }

    //以下作废
//--------------------------------

    @ApiOperation(value = "新增实体数据")
    @PostMapping("add")
    public Map add(@Validated @RequestBody EntUser t) {
//        service.save(t);
        return ResponseDataUtil.returnData("此接口作废,使用batchAdd");
    }
    @ApiOperation(value = "根据主键ID修改实体数据,可局部修改-作废", notes = "修改用户部门（必填）：eu_id,entCode,user_id")
//    @PatchMapping("update")
    public Map update(@RequestBody EntUser t) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        Enterprise byId = enterpriseService.getByCode(t.getEntCode());
        if (byId != null && !loginAppUser.getUser_id().equals(byId.getCreateUserId())) {
            return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
        }
        return ResponseDataUtil.returnData("此接口停用");
    }

    @ApiOperation(value = "根据主键ID修改实体数据-批量,可局部修改-作废", notes = "修改用户部门（必填）：eu_id,entCode,user_id")
//    @PatchMapping("updateBatch")
    public Map updateBatch(@RequestBody List<EntUser> t) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        for (EntUser entUser : t) {
            if (entUser != null && (!loginAppUser.getUser_id().equals(entUser.getCreateUserId()))) {
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
        }
        return ResponseDataUtil.returnData("此接口停用");
    }

    @ApiOperation(value = "根据主键ID删除数据-作废", notes = "必填：eu_id")
//    @DeleteMapping("del/{id}")
    public Map del(@PathVariable Integer id) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntUser byId = service.getById(id);
        if (byId != null && (!loginAppUser.getUser_id().equals(byId.getCreateUserId()))) {
            return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
        }
//        return ResponseDataUtil.returnData(service.removeById(id));
        return ResponseDataUtil.returnData("此接口停用");
    }

}
